﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Application.MainModule.BranchesManagement;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;

namespace DistributedServices.MainModule
{
    public partial class MainModuleService
    {

        public List<Branch> GetBranchsByGroupBranchId(Guid groupBranchId)
        {
            try
            {
                IBranchManagementService branchService = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();

                return branchService.FindBranches(groupBranchId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public Branch GetBranchByBranchId(Guid branchId)
        {
            try
            {
                IBranchManagementService branchService = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();

                return branchService.FindBranchByBranchId(branchId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeBranch(Branch address)
        {
            try
            {
                IBranchManagementService branchService = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
                branchService.ChangeBranch(address);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveBranch(Branch address)
        {
            try
            {
                IBranchManagementService branchService = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
                branchService.RemoveBranch(address);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddBranch(Branch address)
        {
            try
            {
                IBranchManagementService branchService = IoCFactory.Instance.CurrentContainer.Resolve<IBranchManagementService>();
                branchService.AddBranch(address);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

    }
}
